package alibaba.threadlocal;

import lombok.Data;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;

/**
 * @author ChangLiang
 * @date 2020/4/20
 */
@Slf4j
public class SessionHandlerNoThreadLocal {

    @Data
    @ToString
    public static class Session{
        private String id;
        private String user;
        private String status;
    }

    public Session createSession() {
        return new Session();
    }

    public String getUser(Session session) {
        return session.getUser();
    }

    public String getStatus(Session session) {
        return session.getStatus();
    }

    public void setStatus(Session session,String status) {
        session.setStatus(status);
    }

    @Test
    public void test() {
        new Thread(() -> {
            SessionHandlerNoThreadLocal handler = new SessionHandlerNoThreadLocal();
            Session session = handler.createSession();
            handler.getStatus(session);
            handler.getUser(session);
            log.info("user: {}, session:{}",session.getUser(),session.getStatus());
            handler.setStatus(session, "close");
            handler.getStatus(session);
            log.info("user: {}, session:{}",session.getUser(),session.getStatus());
        }).start();
    }
}
